package com.keshan.service.impl;

import com.keshan.mapper.UserDeviceMapper;
import com.keshan.pojo.UserDevice;
import com.keshan.pojo.UserDeviceExample;
import com.keshan.service.UserDeviceServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author keshan
 * @date 2022/11/19 - 21:07
 */
@Service
public class UserDeviceServicesImpl implements UserDeviceServices {

    @Autowired
    UserDeviceMapper userDeviceMapper;

    /**
     * 用户名查询设备
     *
     * @param username
     * @return
     */
    @Override
    public List<UserDevice> getUserDeviceListByUsername(String username) {
        UserDeviceExample userDeviceExample = new UserDeviceExample();
        UserDeviceExample.Criteria criteria = userDeviceExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        criteria.andImeiTypeEqualTo(1);
        return userDeviceMapper.selectByExample(userDeviceExample);
    }

    /**
     * 设备查询用户名
     *
     * @param imei
     * @return
     */
    @Override
    public List<UserDevice> getUserDeviceListByImei(String imei) {
        UserDeviceExample userDeviceExample = new UserDeviceExample();
        UserDeviceExample.Criteria criteria = userDeviceExample.createCriteria();
        criteria.andImeiEqualTo(imei);
        criteria.andImeiTypeEqualTo(1);
        return userDeviceMapper.selectByExample(userDeviceExample);
    }


    /**
     * @param userDevice entity
     * @author tangzheng
     * @date 2023/2/10 18:43
     * @description 通用插入方法
     */
    @Override
    public void insertSelective(UserDevice userDevice) {
        userDeviceMapper.insertSelective(userDevice);
    }

    /**
     * @param imei 设备号
     * @return boolean
     * @author tangzheng
     * @date 2023/2/10 18:32
     * @description 对应设备号是否已被绑定
     */
    @Override
    public boolean isExistByImei(String imei) {
        return userDeviceMapper.isExistByImei(imei) > 0;
    }

    /**
     * @param userDevice 设备类型与用户名
     * @return boolean
     * @author tangzheng
     * @date 2023/2/10 18:32
     * @description 用户是否已经绑定相同类型的设备
     */
    @Override
    public boolean isExitsByTypeAndUser(UserDevice userDevice) {
        return userDeviceMapper.isExitsByTypeAndUser(userDevice) > 0;
    }

    /**
     * @param userDevice 设备号和设备类型和用户名
     * @author tangzheng
     * @date 2023/2/10 18:40
     * @description 用户解绑设备
     */
    @Override
    public void deleteSelective(UserDevice userDevice) {
        userDeviceMapper.deleteSelective(userDevice);
    }
}
